package com.UI;

import java.util.Calendar;
import java.util.List;

import com.Model.StudentDBModel;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.AdapterView.OnItemSelectedListener;

public class AddToDo extends Activity{

    private Button pickTime,pickDate;
    
    private int Day, Month, Year;
    private int Hour, Minute;
    String date;

	String time;    
    String courseNo;
    
    static final int TIME_DIALOG_ID = 1;
	static final int DATE_DIALOG_ID = 0;
	
	
	StudentDBModel model;
	 List<String> records;
	 String [] courses;
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_todo);
        
        
        
        model = new StudentDBModel(this);
        records = model.selectCoursesByNo();
        
        
        // register this activity as a button event listener
        Button btnSubmit = (Button)findViewById(R.id.addToDo);
         btnSubmit.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
        
         		EditText txtTitle = (EditText)findViewById(R.id.txtToDo);
        		
        		String tempTitle = txtTitle.getText().toString().replaceAll(" ", "");
        		
        		
        		if(tempTitle.length() == 0) {
          	      
         		   AlertDialog alertDialog = new AlertDialog.Builder(AddToDo.this).create();	        			
         		   alertDialog.setTitle("Warning");	        		  
         		   alertDialog.setMessage("Please enter a Title for your TO-DO");	        		  	        		  
         		   alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
         		  
         		   public void onClick(DialogInterface dialog, int which) {
        		       return;
        		  } }); 
        		  alertDialog.show();
        		  
        		}
        		
        		else if(courseNo == null ) {
          		   AlertDialog alertDialog = new AlertDialog.Builder(AddToDo.this).create();	        			
         		   alertDialog.setTitle("Warning");	        		  
         		   alertDialog.setMessage("Please add a 'Course' first, To add TO-DO");	        		  	        		  
         		   alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
         		  
         		   public void onClick(DialogInterface dialog, int which) {
        		       return;
        		  } }); 
        		  alertDialog.show();
        			
        		}
        		
        		else {
	        		// collect the course data
	        		final ContentValues ToDo = new ContentValues();
	
	        		EditText txtToDo = (EditText)findViewById(R.id.txtToDo);
	        		
	        		date = (Month + 1) + "/" + Day;
	
	        		time = pad(Hour) + ":" + pad(Minute);
	        		
	        		
	        		
	        		ToDo.put("CourseId", courseNo);
	        		ToDo.put("title", txtToDo.getText().toString());
	        		ToDo.put("Desc", txtToDo.getText().toString()+courseNo);
	        		ToDo.put("date", date);
	        		ToDo.put("time", time);
	        		
	        		
	        		// use the model to store the course data
	        		model.addToDo(ToDo);
	        		
	                		
	        		//once the course is added, redirect the user to the View Courses page
	        		Intent viewToDo = new Intent(AddToDo.this, ToDoList.class);
					startActivity(viewToDo);
					AddToDo.this.finish();
	             }
	             
             }
	             
	         });
             
            
        Spinner s = (Spinner) findViewById(R.id.spinnerToDo);
        ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, records);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        s.setAdapter(adapter);
        s.setOnItemSelectedListener(new MyOnItemSelectedListener());
    

        pickDate = (Button) findViewById(R.id.dueDate);
		pickTime = (Button) findViewById(R.id.dueTime);


        
		// add a click listener to the button
		pickDate.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				showDialog(DATE_DIALOG_ID);
			}
		});
		
		// add a click listener to the button
		pickTime.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				showDialog(TIME_DIALOG_ID);
			}
		});
		
		// get the current date
		final Calendar c1 = Calendar.getInstance();
		Year = c1.get(Calendar.YEAR);
		Month = c1.get(Calendar.MONTH);
		Day = c1.get(Calendar.DAY_OF_MONTH);
		

        // get the current time
        final Calendar c2 = Calendar.getInstance();
        Hour = c2.get(Calendar.HOUR_OF_DAY);
        Minute = c2.get(Calendar.MINUTE);
  
   }
    
    
    
    public class MyOnItemSelectedListener implements OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> parent,
            View view, int pos, long id) {
                 courseNo = parent.getItemAtPosition(pos).toString();
        }

        public void onNothingSelected(AdapterView parent) {
          // Do nothing.
        }

   }  
    
    
    
	private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			Hour = hourOfDay;
			Minute = minute;
		}
	};

	// the call back received when the user "sets" the date in the dialog
	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			Year = year;
			Month = monthOfYear;
			Day = dayOfMonth;

		}
	};


    
	
	private static String pad(int c) {
		if (c >= 10)
			return String.valueOf(c);
		else
			return "0" + String.valueOf(c);
	}

	
	@Override
	protected Dialog onCreateDialog(int id) {
		if (id == DATE_DIALOG_ID) {
			return new DatePickerDialog(this, mDateSetListener, Year, Month,
					Day);
		} 
		else if (id == TIME_DIALOG_ID) {
			return new TimePickerDialog(this, mTimeSetListener, Hour, Minute,
					false);
		}
		return null;
	}
	
	
}
